//
//  JKRFeedProgressLayout.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/10.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedProgressLayout.h"

@implementation JKRFeedProgressLayout

- (instancetype)initWithModel:(JKRFeedDataModel *)model {
    self = [super init];
    
    if (model.order.order_type == 1) { // 陪玩
        switch (model.order.order_status) {
            case 1: // 已发布
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 4: // 超时无人购买
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 6: // 老板买单
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 7: // 超时未填写结果
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 8: // 填写结果-打了
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 9: // 填写结果-没打
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 10: // 填写打了超时未确认或者确认服务
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 11: // 发起投诉
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 12: // 申诉成功
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 13: // 申诉失败
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            default:
                break;
        }
    } else if (model.order.order_type == 2) { // 约玩
        switch (model.order.order_status) {
            case 1: // 已发布
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if(isSignUp) { // 报名者
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 2: // 超时无人报名
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if(isSignUp) { // 报名者
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 3: // 超时未选大神
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if(isSignUp) { // 报名者
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 5: // 选择大神
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 7: // 超时未填写结果
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 8: // 填写结果-打了
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 9: // 填写结果-没打
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 10: // 填写打了超时未确认或者确认服务
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 11: // 发起投诉
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 12: // 申诉成功
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 13: // 申诉失败
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = YES;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            default:
                break;
        }
    }
    
    if (!self.show) {
        return self;
    }
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_main_queue(), ^{
        self.subViews = [NSMutableArray array];
        NSArray *titles = model.order.tracking.tracking_zh;
        NSInteger progress = model.order.tracking.index;
        
        CGFloat padding = 30.f;
        CGFloat imageWH = 25.f;
        CGFloat margin = (kScreenWidth - imageWH * titles.count - padding * 2) / (titles.count - 1);
        
        for (NSUInteger i = 0; i < titles.count; i++) {
            UIImageView *imageView = [UIImageView new];
            imageView.image = [JKRFeedDetailHelper progressImageWithProgressTitle:titles[i] highLight:(i <= progress)];
            CGFloat x = padding + (imageWH + margin) * i;
            imageView.frame = CGRectMake(x, 10, imageWH, imageWH);
            
            UILabel *titleLabel = [UILabel new];
            titleLabel.font = [UIFont systemFontOfSize:12];
            titleLabel.textColor = (i <= progress) ? UIColorHex(262626) : UIColorHex(D8D8D8);
            titleLabel.text = titles[i];
            [titleLabel sizeToFit];
            
            {
                CGFloat x = (imageView.frame.origin.x + imageView.frame.size.width * 0.5) - (titleLabel.frame.size.width * 0.5);
                titleLabel.frame = CGRectMake(x, 39, titleLabel.frame.size.width, titleLabel.frame.size.height);
            }
            
            UIView *line = [UIView new];
            if (i > 0) {
                if (i <= progress) {
                    if ([titles[i] isEqualToString:@"申诉中"] || [titles[i] isEqualToString:@"已退款"]) {
                        line.backgroundColor = UIColorHex(F4AD3A);
                    } else {
                        line.backgroundColor = UIColorHex(CE0008);
                    }
                } else {
                    line.backgroundColor = UIColorHex(D8D8D8);
                }
                
                CGFloat x = imageView.frame.origin.x - margin;
                line.frame = CGRectMake(x - 2, 22, margin + 4, 1);
            }
            
            [self.subViews addObject:imageView];
            [self.subViews addObject:titleLabel];
            [self.subViews insertObject:line atIndex:0];
        }
        dispatch_semaphore_signal(semaphore);
    });
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    return self;
}

@end
